package cn.dogger.easyboot.system.service.impl;


import cn.dogger.easyboot.common.exception.RollBackException;
import cn.dogger.easyboot.redis.redistemplate.RedisUtils;
import cn.dogger.easyboot.system.domain.bo.SysUserBO;
import cn.dogger.easyboot.system.domain.dto.SysUserDTO;
import cn.dogger.easyboot.system.domain.entity.SysUser;
import cn.dogger.easyboot.system.domain.enums.SysUserDelFlag;
import cn.dogger.easyboot.system.domain.vo.SysUserDetail;
import cn.dogger.easyboot.system.domain.vo.SysUserVO;
import cn.dogger.easyboot.system.mapper.SysUserMapper;
import cn.dogger.easyboot.system.service.SysUserService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
* 用户信息表
* @author dogger
* 2022-04-16
*/
@Service
public class SysUserServiceImpl  extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
        final Logger log ;
        private final String SYS_USER_CACHE_ = SysUser.SYS_USER_CACHE_;
        private final RedisUtils<String, SysUser> redisUtils;
         private final SysUserMapper mapper;

         public SysUserServiceImpl(SysUserMapper mapper,RedisUtils<String, SysUser> redisUtils) {
             this.mapper = mapper;
             this.redisUtils = redisUtils;
             log = LoggerFactory.getLogger(getClass());
             setSysUserCache();
         }

    @Override
    public List<SysUserVO> page(SysUserDTO dto) {
        return mapper.list(dto);
    }


    @Override
    @Transactional(rollbackFor = RollBackException.class)
    public void add(SysUserBO bo) {
        SysUser entity = BeanUtil.copyProperties(bo, SysUser.class);
        mapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = RollBackException.class)
    public void edit(SysUserBO bo) {
        SysUser entity = BeanUtil.copyProperties(bo, SysUser.class);
        mapper.updateById(entity);
    }

    @Override
    public SysUserDetail detailById(String id) {
        List<SysUser> sysUserCache = getSysUserCache();
        List<SysUser> list = sysUserCache.stream().filter(user -> user.getUserId().equals(id)).toList();
        SysUser entity = list.get(0);
        return BeanUtil.toBean(entity, SysUserDetail.class);
    }

    @Override
    public void removeByIds(String[] ids) {
        List<String> list = Arrays.asList(ids);
        mapper.deleteBatchIds(list);
    }

    private void setSysUserCache() {
        if (redisUtils.hasKey(SYS_USER_CACHE_)) {
            redisUtils.delete(SYS_USER_CACHE_);
        }
        List<SysUser> list = new LambdaQueryChainWrapper<>(mapper)
                .eq(SysUser::getDelFlag, SysUserDelFlag.EXIST.getCode())
                .list();
        if(list != null && list.size()>0){
            redisUtils.setList(SYS_USER_CACHE_, -1,list);
            log.info("用户集合缓存成功！");
        }else{
            log.error("用户集合为空！");
        }

    }

    /**
     * get用户集合
     * @return 用户集合
     */
    public List<SysUser> getSysUserCache(){
        if(!redisUtils.hasKey(SYS_USER_CACHE_)){
            setSysUserCache();
        }
        return redisUtils.getList(SYS_USER_CACHE_,0,-1);
    }
}